package com.lml.Utils

import android.content.Context
import android.content.Intent
import androidx.activity.result.ActivityResultLauncher

/**
 * 使用泛型实化优化Activity的启动
 */

/**使用案例
 * startActivity<AnotherActivity>(this)
 */
inline fun <reified T> startActivity(context: Context) {
    val intent = Intent(context, T::class.java)
    context.startActivity(intent)
}


/**使用案例
 * startActivity<AnotherActivity>(this) {
 *     putExtra("param1", "data")
 *     putExtra("param2", "123")
 * }
 */
inline fun <reified T> startActivity(context: Context, block: Intent.() -> Unit) {
    val intent = Intent(context, T::class.java)
    intent.block()
    context.startActivity(intent)
}

/**使用案例
 * startActivity<AnotherActivity>(this, activityResultLauncher) {
 *     putExtra("param1", "data")
 *     putExtra("param2", "123")
 * }
 */
inline fun <reified T> startActivity(context: Context, activityResultLauncher: ActivityResultLauncher<Intent>, block: Intent.() -> Unit) {
    val intent = Intent(context, T::class.java)
    intent.block()
    activityResultLauncher.launch(intent)
}